package com.cis467.mrball.backend;

import com.cis467.mrball.parts.LevelWrapper;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MrBallSQLiteOpenHelper extends SQLiteOpenHelper {

    MrBallSQLiteOpenHelper(Context context) {
        super(context, DatabaseConstants.DATABASE_NAME, null, DatabaseConstants.DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
    	
    	String createTableLevel = "CREATE TABLE IF NOT EXISTS " +
        		DatabaseConstants.DATABASE_NAME 			+ "." +
        		DatabaseConstants.TABLE_LEVELS 				+ 
        		"( _id INTEGER PRIMARY KEY AUTOINCREMENT," 	+ 
        		DatabaseConstants.FIELD_CREATOR 			+ DatabaseConstants.TEXT 	+ "," +
        		DatabaseConstants.FIELD_LEVEL_NAME 			+ DatabaseConstants.TEXT 	+ "," +
        		DatabaseConstants.FIELD_LEVEL_BATCH 		+ DatabaseConstants.TEXT 	+ "," +
        		DatabaseConstants.FIELD_LEVEL_NUMBER 		+ DatabaseConstants.INTEGER + "," +
        		DatabaseConstants.FIELD_LEVEL_XML 			+ DatabaseConstants.TEXT 	+ "," +
        		DatabaseConstants.FIELD_PERSONAL_BEST 		+ DatabaseConstants.INTEGER + "," +
        		DatabaseConstants.FIELD_WORLD_RECORD 		+ DatabaseConstants.INTEGER + "," +
        		DatabaseConstants.FIELD_LOCAL_VOTE 			+ DatabaseConstants.INTEGER + "," +
        		DatabaseConstants.FIELD_SERVER_VOTE 		+ DatabaseConstants.INTEGER + "," +
        		DatabaseConstants.FIELD_UPVOTES 			+ DatabaseConstants.INTEGER + "," +
        		DatabaseConstants.FIELD_DOWNVOTES 			+ DatabaseConstants.INTEGER + " " +
        		DatabaseConstants.CONSTRAINT_1 				+ " UNIQUE ( " +
        		DatabaseConstants.FIELD_CREATOR 			+ ", " +
        		DatabaseConstants.FIELD_LEVEL_NAME 			+ ", " +
        		DatabaseConstants.FIELD_LEVEL_BATCH 			+ ") ON CONFLICT IGNORE, " +
        		DatabaseConstants.CONSTRAINT_2 				+ " UNIQUE ( " +
        		DatabaseConstants.FIELD_CREATOR 			+ ", " +
        		DatabaseConstants.FIELD_LEVEL_BATCH 			+ ", " +
        		DatabaseConstants.FIELD_LEVEL_NUMBER 			+ ") ON CONFLICT IGNORE, " + ");";    	
    	
        db.execSQL(createTableLevel);
    }

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// Do nothing.. for now
	}

	@Override
	public void onOpen(SQLiteDatabase db) {
	}
}
